<?php

namespace backend\models;

use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use common\service\ConstatService ;

/**
 * This is the model class for table "admin_user".
 *
 * @property string $user_id
 * @property integer $type_id
 * @property string $username
 * @property string $phone
 * @property string $password
 * @property string $password_order
 * @property integer $status
 * @property integer $uptime
 * @property string $create_user_id
 * @property string $role
 * @property integer $login_error
 * @property string $login_date
 * @property string $avatar
 */
class AdminUser extends ActiveRecord implements IdentityInterface {
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'admin_user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type_id', 'status', 'uptime', 'create_user_id','login_error'], 'integer'],
            [['username'], 'string', 'max' => 30],
            [['phone'], 'string', 'max' => 11],
            [['password', 'role'], 'string', 'max' => 50],
            [['password_order'], 'string', 'max' => 6],
            [['phone'], 'unique'],
            [['login_date'], 'string'],
            [['token'],'safe'],
            [['avatar'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(){
        return [
            'user_id' => 'User ID',
            'type_id' => '用户类型',
            'username' => '用户名称',
            'phone' => '手机号码',
            'password' => '密码',
            'password_order' => '用户加密密钥',
            'status' => '用户状态',
            'uptime' => '更新时间',
            'create_user_id' => '创建人id',
            'role' => '角色',
            'avatar' => '头像' ,
        ];
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($user_id) {
        return static::findOne(['user_id' => $user_id]);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = NULL) {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findByUsername($username) {
        return static::findOne(['username' => $username]);
    }

    /**
     * Finds user by password reset token
     *
     * @param string $token password reset token
     * @return static|null
     */
    public static function findByPasswordResetToken($token) {
//        if (!static::isPasswordResetTokenValid($token)) {
//            return null;
//        }
//
//        return static::findOne([
//                    'password_reset_token' => $token,
//                    'status' => self::STATUS_ACTIVE,
//        ]);
    }

    /**
     * Finds out if password reset token is valid
     *
     * @param string $token password reset token
     * @return boolean
     */
    public static function isPasswordResetTokenValid($token) {
//        if (empty($token)) {
//            return false;
//        }
//        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
//        $parts = explode('_', $token);
//        $timestamp = (int) end($parts);
//        return $timestamp + $expire >= time();
    }

    /**
     * @inheritdoc
     */
    public function getId() {
        return $this->getPrimaryKey();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey() {
        return $this->auth_key;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey) {
        return $this->getAuthKey() === $authKey;
    }

    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password) {
        return Yii::$app->security->validatePassword($password, $this->password_hash);
    }

    /**
     * Generates password hash from password and sets it to the model
     *
     * @param string $password
     */
    public function setPassword($password) {
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
    }

    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey() {
        $this->auth_key = Yii::$app->security->generateRandomString();
    }

    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken() {
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
    }

    /**
     * Removes password reset token
     */
    public function removePasswordResetToken() {
        $this->password_reset_token = null;
    }

    public static function findAdminUser(){
        $user_list = AdminUser::findAll() ;
        return $user_list;
    }

    public static function deleteAdminUser($id)
    {
        $delete = AdminUser::findOne(["user_id"=>$id])->delete();
        if($delete){
            $msg = ['msg'=>'用户已成功删除'];
        }else{
            $msg = ['msg'=>'用户删除失败'];
        }
        return $msg;
    }
}
